package com.bamboo.custom.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.bamboo.custom.R;

/**
 * 项目名称：
 *
 * @Author bamboolife
 * 邮箱：core_it@163.com
 * 创建时间：2021/9/9 12:18 上午
 * 描述：折线图
 */
public class LineCharView1 extends View {
    private String xValues[]={"大","中","小"};
    private String yValues[]={"大","中","小"};

    //x轴颜色
    private int xAxesColor= R.color.black;
    //x轴文字颜色
    private int xAxesTextColor= R.color.black;
    //x轴文字大小
    private int xAxesTextSize;
    //x轴的间隔或单位长度
    private int xScale;
    //x轴是否显示,默认显示
    private boolean isXVisible=true;
    //x轴上线移动的距离
    private int xMoveLength=20;
    //x轴的宽度
    private int xBoldWidth=3;
    //y轴颜色
    private int yAxesColor= R.color.black;
    //y轴文字颜色
    private int yAxesTextColor= R.color.black;
    //y轴文字大小
    private int yAxesTextSize;
    //y轴是否显示
    private boolean isYVisible=true;
    //折线颜色
    private int lineColor= R.color.black;
    //网格线的颜色
    private int gridLineColor;

    //整个画布的宽、高
    private int width;
    private int height;
    //坐标原点位置
    private int originX = 0;
    private int originY = 0;
    // 默认边距
    private int margin = 20;
    // 距离左边偏移量
    private int marginX = 30;
    // 画笔
    private TextPaint mTextPaint; //画文字
    private Paint paintAxes;            //画轴
    private Paint paintCoordinate;      //画坐标（文字或数值）
    private Paint paintLine;           //画折线
    private Paint paintGridLine;//画网格线
    private int charHeght=0;

    public LineCharView1(Context context) {
        super(context);
        init(null, 0);

    }

    public LineCharView1(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public LineCharView1(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.LineCharView, defStyle, 0);

        a.recycle();
        initPaint();


    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width=getWidth();
        height=getHeight();
        originX=margin+marginX;
        originY=height-margin;
        charHeght=height-margin-xMoveLength;

        Log.i("log_size", "onLayout: width="+width+",height="+height);
        Log.i("log_size", "onLayout: MeasuredWidth="+getMeasuredWidth()+",MeasuredHeight="+getMeasuredHeight());
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.i("log_size", "onSizeChanged: w="+w+",h="+h+",oldw="+oldw+",pldh="+oldh);
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        mTextPaint = new TextPaint();
        mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextAlign(Paint.Align.LEFT);


        paintAxes=new Paint();
        paintAxes.setAntiAlias(true);//抗锯齿
        paintAxes.setStyle(Paint.Style.STROKE);//描边
        paintAxes.setDither(true);//放抖动。绘制出来的图形比较柔和。暂时先这么理解
        paintAxes.setColor(Color.BLACK);
        paintAxes.setStrokeWidth(xBoldWidth);

        paintGridLine=new Paint();
        paintGridLine.setAntiAlias(true);//抗锯齿
        paintGridLine.setStyle(Paint.Style.STROKE);//描边
        paintGridLine.setDither(true);//放抖动。绘制出来的图形比较柔和。暂时先这么理解
        paintGridLine.setColor(Color.BLACK);
        paintGridLine.setStrokeWidth(xBoldWidth);

        paintCoordinate = new Paint(Paint.ANTI_ALIAS_FLAG);    //画坐标（文字或数值）
        paintCoordinate.setColor(ContextCompat.getColor(getContext(), xAxesTextColor));
        paintCoordinate.setTextSize(22f);

        paintLine = new Paint();
        paintLine.setColor(ContextCompat.getColor(getContext(), lineColor));
        paintLine.setStyle(Paint.Style.FILL);
        paintLine.setDither(true);
        paintLine.setAntiAlias(true);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
         drawAxesLine(canvas,paintAxes);
         drawGridLine(canvas,paintGridLine);
        // TODO: consider storing these as member variables to reduce
        // allocations per draw cycle.
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();

        int contentWidth = getWidth() - paddingLeft - paddingRight;
        int contentHeight = getHeight() - paddingTop - paddingBottom;

    }

   private void drawAxesLine(Canvas canvas,Paint paint){
     //画x轴
       canvas.drawLine(originX+xMoveLength,originY-xMoveLength,width-originX+xMoveLength-getPaddingRight(),originY-xMoveLength,paint);
   }

   private void drawGridLine(Canvas canvas,Paint paint){
       int intervalY=charHeght/yValues.length;
       for (int i = 0; i <yValues.length; i++) {
           canvas.drawLine(originX+xMoveLength,0+xMoveLength+i*intervalY,width-originX+xMoveLength-getPaddingRight(),xMoveLength+i*intervalY,paint);
       }

   }
}